// koa
const Koa = require('koa')

// 引入config配置
const config = require('./config')

// 解决跨域问题
const cors = require('koa2-cors')

// 解析前端参数
const bodyParser = require('koa-bodyparser')

// 连接数据库
const mongoose = require('mongoose')

const app = new Koa()

mongoose.connect(config.db, { useNewUrlParser: true }, err => {
  if (!err) {
    console.log('Connecting database successfully')
  } else {
    console.log('Failed to connect to database')
  }
})

app.use(cors())
app.use(bodyParser())

const user_router = require('./routers/api/user_router')
const course_router = require('./routers/api/course_router')
const school_router = require('./routers/api/school_router')
const example_router = require('./routers/api/example_router')

app.use(user_router.routes()).use(user_router.allowedMethods())
app.use(course_router.routes()).use(course_router.allowedMethods())
app.use(school_router.routes()).use(school_router.allowedMethods())
app.use(example_router.routes()).use(example_router.allowedMethods())

app.listen(config.port)
